<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"

    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="问题标题" prop="problemTitle">
        <el-input
          v-model="dataForm.problemTitle"
          placeholder="问题标题"
        ></el-input>
      </el-form-item>

<!--      <el-form-item label="发表学生" prop="studentName">-->
<!--        <el-input-->
<!--          v-model="dataForm.studentName"-->
<!--          placeholder="发表学生"-->
<!--        ></el-input>-->
<!--      </el-form-item>-->

      <el-form-item label="问题封面" prop="problemHover">
        <div class="avatar-container">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:8888/api/minio/upload"
            :show-file-list="false"
            :on-preview="handlePictureCardPreview"
            :on-success="handleUploadSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img style="width: 150px;height: 150px"  v-if="dataForm.problemHover" :src="dataForm.problemHover" class="avatar" alt="">
            <el-button v-else size="small" type="primary">点击上传</el-button>
          </el-upload>
        </div>
      </el-form-item>

      <el-form-item style="margin-top: 100px" label="文章描述" prop="problemDescription">
        <el-input
          v-model="dataForm.problemDescription"
          placeholder="问题描述"
        ></el-input>
      </el-form-item>


      <el-form-item label="问题类型" prop="problemTypeName">
        <el-select v-model="dataForm.problemTypeName" placeholder="请选择">
          <el-option
            v-for="item in problemTypeOption"
            :key="item.id"
            :label="item.problemTypeName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import th from 'element-ui/src/locale/lang/th'

export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        problemTitle: '',
        studentName: '',
        problemTypeName: '',
        createTime: '',
        updateTime: '',
        isDelete: '',
        problemHover:''
      },
      problemTypeOption: [],
      dataRule: {
        problemTitle: [
          { required: true, message: '问题标题不能为空', trigger: 'blur' }
        ],
        problemTypeName: [
          {
            required: true,
            message: '问题发表的类型不能为空',
            trigger: 'blur'
          }
        ],
        createTime: [
          { required: false, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: false, message: '修改时间不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    init (id) {
      this.getProblemTypeList()
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/problem/problem/info/${this.dataForm.id}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.problemTitle = data.problem.problemTitle;
              this.dataForm.studentName = data.problem.studentName;
              this.dataForm.problemTypeName = data.problem.problemTypeName;
              this.dataForm.problemDescription = data.problemAnswer.problemDescription;
              this.dataForm.createTime = data.problem.createTime;
              this.dataForm.problemHover = data.problem.problemHover;
              this.dataForm.updateTime = data.problem.updateTime;
              this.dataForm.isDelete = data.problem.isDelete;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.setProblemTypeId()
          this.$http({
            url: this.$http.adornUrl(
              `/problem/problem/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              problemTitle: this.dataForm.problemTitle,
              studentName: this.dataForm.studentName,
              problemTypeName: this.dataForm.problemTypeName,
              problemDescription: this.dataForm.problemDescription,
              createTime: this.dataForm.createTime,
              problemHover: this.dataForm.problemHover,
              updateTime: this.dataForm.updateTime,
              isDelete: this.dataForm.isDelete
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit('refreshDataList');
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 获取课程类型列表
    getProblemTypeList () {
      this.$http({
        url: this.$http.adornUrl(
          '/problem/problemtype/findProblemTypeList'
        ),
        method: 'get'
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data.problemTypeList);
          this.problemTypeOption = data.problemTypeList;
        } else {
          this.problemTypeOption = [];
        }
      });
    },
    setProblemTypeId () {
      if (typeof this.dataForm.problemTypeName === 'string') {
        for (var i = 0; i < this.problemTypeOption.length; i++) {
          if (
            this.dataForm.id &&
            this.dataForm.problemTypeName === this.problemTypeOption[i].problemTypeName
          ) {
            this.dataForm.problemTypeName = this.problemTypeOption[i].id;
          }
          console.log(this.dataForm.problemTypeName, 'sadas');
        }
      }
    },
    handlePreview (file) {
      console.log(file);
    },

    handleRemove (file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview (file) {
      console.log(file);
      this.dataForm.problemHover = file.url;
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleUploadSuccess (res, file) {
      console.log(res, 'res')
      console.log(file, 'file')
      this.dataForm.problemHover = res.msg

      // this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload (file) {
      console.log(file.type, 'file')
      const isJPG = file.type === 'image/jpeg';
      const isPng = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isPng) {
        this.$message.error('上传头像图片只能是 JPG或PNG格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return (isJPG || isPng) && isLt2M;
    }
  }
};
</script>
<style>
.avatar-container {
  display: flex;
}
</style>
